package com.example.demo.validator;

import com.example.demo.entity.pojo.user.UserPoJo;
import com.example.demo.entity.request.user.UserRequest;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * .
 *
 * @author Mei Ruoxiao
 * @since 2020/8/31
 */
@Component
public class UserValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return clazz.isAssignableFrom(UserRequest.class) ||
                clazz.isAssignableFrom(UserPoJo.class);
    }

    @Override
    public void validate(Object target, Errors errors) {
        UserRequest request = (UserRequest) target;
//        if (request == null) {
//            errors.rejectValue(null, "User.null", "用户 不能为空");
//        } else if (StringUtils.isEmpty(request.getName())) {
//            errors.rejectValue("name", "User.name.null", "名称不能为空");
//        } else if (StringUtils.isEmpty(request.getPassword())) {
//            errors.rejectValue("password", "User.password.null", "密码不能为空");
//        }
    }
}
